/**
 * Created by yunpeng on 2015/4/14.
 */
var express = require('express');
var router = express.Router();
var Users = require('../models/Users').Users;
var Transfer = require('../models/Transfer').Transfer;
var Advice = require('../models/Advice').Advice;
var Count = require('../models/Count').Count;
var request = require('request');
var fs = require('fs');
var gm = require('gm');//处理图片
router.get('/manage',function(req,res){
    res.render('login',{msg:''});
});

router.post('/login', function(req, res) {//用户登录
    new Users().findUser(req.body.username,req.body.password,3,function(result){
        if(result==401){
            res.render('login',{msg:'登录失败'});
        }else{
            res.render('index',result);

        }

    });
});

router.post('/usersList',function(req,res){
    var userid = req.user.id;
    var params = [userid,req.body.date1,req.body.date2,req.body.phone,Number(req.body.type)];
    var page = [req.body.sort,req.body.order,(Number(req.body.page)-1)*Number(req.body.rows),Number(req.body.rows)];

    new Users().getList(params,page,function(result){
        res.send(result);
    });
})

router.post('/ordersList',function(req,res){
    var params = [req.body.orderno,req.body.date1,req.body.date2,req.body.phone,req.body.ostatus];
    var page = [req.body.sort,req.body.order,(Number(req.body.page)-1)*Number(req.body.rows),Number(req.body.rows)];

    new Transfer().getOrderList(params,page,function(result){
        res.send(result);
    });
})

router.post('/bidList',function(req,res){
    var params = [req.body.date1,req.body.date2,req.body.phone,req.body.status,req.query.inforid];
    var page = [req.body.sort,req.body.order,(Number(req.body.page)-1)*Number(req.body.rows),Number(req.body.rows)];

    new Transfer().bidList(params,page,function(result){
        res.send(result);
    });
})

router.post('/adviceList',function(req,res){
    var params = [req.body.date1,req.body.date2,req.body.phone];
    var page = [req.body.sort,req.body.order,(Number(req.body.page)-1)*Number(req.body.rows),Number(req.body.rows)];

    new Advice().getAdviceList(params,page,function(result){
        res.send(result);
    });
})
router.post('/audit',function(req,res){
    var type=req.body.type;
    var id=req.body.id;
    var phone=req.body.phone;
    var authstatetype=req.body.authstate;
    new Users().Audit(id,type,authstatetype,function(data){
        if(data){
            var str='';
            if(type==1){
                str='亲，恭喜您，您的认证已经通过审核。';
            }else{
                str='亲，很抱歉，您的认证未通过审核。'
            }
            request.post({url:'http://222.73.117.158:80/msg/HttpBatchSendSM',
                    form: {account:'vip_ycgj',pswd:'Tch123456',mobile:phone,msg:str,needstatus:true}},
                function optionalCallback(err, httpResponse, body) {
                    res.send(true);
                });
        }else{
            res.send(false);
        }
    });
});
router.post('/countList',function(req,res){
    var params = [req.body.date1,req.body.date2,req.body.phone];
    var page = [req.body.sort,req.body.order,(Number(req.body.page)-1)*Number(req.body.rows),Number(req.body.rows)];

    new Count().getCountList(params,page,function(result){
        res.send(result);
    });
});

router.get('/getIdxFiles',function(req,res){
    var edittype = req.query.edittype;
    if(edittype==1){
        fs.readdir('public/javascripts/slider/img/sls/',function(err,files){
            //res.setHeader('Content-Type','text/html');
            res.send(files);
        });
    }else if(edittype==2){
        fs.readdir('public/javascripts/slider/img/sls1/',function(err,files){
            //res.setHeader('Content-Type','text/html');
            res.send(files);
        });
    }

});

router.post('/editAppIdx',function(req,res){
    var img = req.body.oldidx;//更新之前的图片路径
    var edittype = req.body.edittype;
    res.setHeader('Content-Type','text/html');
    if(req.files.files){//判断图片需不需要更新
        //var coords = req.body.coords;
        var imgPath = '/javascripts/slider/img/sls/'+img;//req.body.dataWidth
        if(edittype==2){
            imgPath = '/javascripts/slider/img/sls1/'+img;//req.body.dataWidth
        }

        gm(req.files.files.path).crop(req.body.dataWidth, req.body.dataHeight, req.body.dataX, req.body.dataY)
            .resize(600, 600)//强制此宽高缩放，不是1比1的话会拉伸，不加！号可保持纵横比
            .noProfile()
            .write('public'+imgPath, function (err) {
                if (!err){
                    fs.unlinkSync(req.files.files.path);//删除upload下的文件
                    /*if(img&&img!=''){
                        console.log('删除'+img);
                        fs.unlinkSync('public'+img);//删除原来的图片
                    }*/
                    res.send(true);
                }else{
                    res.send(false);
                }
            });
    }else{
        res.send(false);
    }
});

module.exports = router;